package com.dongzili.demo.customer.redis.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongzili.demo.common.config.mybatisplus.MybatisPlusUtil;
import com.dongzili.demo.common.config.redis.RedisUtil;
import com.dongzili.demo.common.vo.ApiResponse;
import com.dongzili.demo.customer.redis.model.InterfaceLog;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/test")
public class TestController {


    @GetMapping("redis/sync")
    public ApiResponse<String> sync() {
        int page = 1;
        int size = 10;
        Page<InterfaceLog> pageData = MybatisPlusUtil.getMapper(InterfaceLog.class).selectPage(new Page<>(page, size), null);
        addOrUpdateBatch(pageData.getRecords());
        for (int i = 2; i < pageData.getPages(); i++) {
            pageData = MybatisPlusUtil.getMapper(InterfaceLog.class).selectPage(new Page<>(i, size), null);
            addOrUpdateBatch(pageData.getRecords());
        }
        return ApiResponse.success();
    }

    /**
     * mysql分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("mysql/page")
    public ApiResponse<List<InterfaceLog>> page1(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<InterfaceLog> pageData = MybatisPlusUtil.getMapper(InterfaceLog.class).selectPage(new Page<>(page, size), null);
//        List<InterfaceLog> list = redisService.findUsersByPage(InterfaceLog.class, page, size);
        return ApiResponse.success(pageData.getRecords());
    }

    /**
     * redis分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("redis/page")
    public ApiResponse<List<InterfaceLog>> page2(@RequestParam(value = "keyword", defaultValue = "") String keyword, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
        List<InterfaceLog> list = RedisUtil.getHashService(InterfaceLog.class).findByPage(keyword, page, size);
        return ApiResponse.success(list);
    }

    /**
     * redis分页排序查询,只能是Set类型数据
     * @param page
     * @param size
     * @return
     */
    @GetMapping("redis/findByPageAndSorted")
    public ApiResponse<List<InterfaceLog>> findByPageAndSorted(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
        List<InterfaceLog> list = RedisUtil.getSetService(InterfaceLog.class).findByPageAndSorted(page, size);
        return ApiResponse.success(list);
    }

    /**
     * 插入数据
     * @param interfaceLog
     * @return
     */
    @PostMapping("addOrUpdate")
    public ApiResponse<?> addOrUpdate(@RequestBody InterfaceLog interfaceLog){
        RedisUtil.getHashService(InterfaceLog.class).addOrUpdate(interfaceLog);
        RedisUtil.getSetService(InterfaceLog.class).addOrUpdate(interfaceLog);
        return ApiResponse.success();
    }

    @PostMapping("addOrUpdateBatch")
    public ApiResponse<?> addOrUpdateBatch(@RequestBody List<InterfaceLog> values){
        RedisUtil.getHashService(InterfaceLog.class).addOrUpdateBatch(values);
        for (InterfaceLog interfaceLog : values){
            RedisUtil.getSetService(InterfaceLog.class).addOrUpdate(interfaceLog);
        }
        return ApiResponse.success();
    }

    /**
     * 查询数据
     * @param id
     * @return
     */
    @GetMapping("redis/get")
    public ApiResponse<InterfaceLog> get(@RequestParam(value = "id") String id) {
        InterfaceLog log = RedisUtil.getHashService(InterfaceLog.class).get(id);

//        HashOperations<String, String, InterfaceLog> hashOps = RedisUtil.getHashOperations();
//        InterfaceLog interfaceLog = hashOps.get("hashKey", id);

        return ApiResponse.success(log);
    }

    @GetMapping("redis/search")
    public ApiResponse<Set<InterfaceLog>> search(@RequestParam(value = "keyword") String keyword, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Set<InterfaceLog> set = RedisUtil.getSetService(InterfaceLog.class).searchKeys(keyword, page, size);
        return ApiResponse.success(set);
    }


}
